package com.snailhd.ss.box;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.snailhd.common.util.FileHandle;
import com.snailhd.common.util.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * function getImgSrc(text) {
 *     var t3s = ["1026", "382"];
 *     var t4s = ["104122", "104138"];
 *     var t5s = ["692", "156207"];
 *     var _text = {
 *         type3: "https://" + t3s[Math.floor(Math.random() * t3s.length)] + ".c13579.com",
 *         type4: "https://" + t4s[Math.floor(Math.random() * t4s.length)] + ".c13579.com",
 *         type5: "https://" + t5s[Math.floor(Math.random() * t5s.length)] + ".c13579.com"
 *     };
 *     return text.replace(/type3|type4|type5/gi,
 *     function(s, t) {
 *         return _text[s]
 *     })
 * }
 *
 * @author hd
 * @date 2020-02-10 23:18
 */

@Slf4j
public class crawlerBox {

    public static void main(String[] args) throws Exception{

        //t:1，图片，2视频，4影视
        String str = HttpClientUtil.get("http://www.9999box.com/box/show/json?id=1088233&t=4&page=1");
    }


    public void main() throws Exception {
        List<String> list = new ArrayList<>();
        for (int page=42;page<=101;page++){
            String str = HttpClientUtil.get("http://www.9999box.com/box/top/json?page="+page+"&by=1");
            JSONObject obj = JSON.parseObject(str);
            JSONArray items = obj.getJSONObject("data").getJSONArray("items");
            for(int i=0;i<items.size();i++){
                JSONObject item = items.getJSONObject(i);
                list.add(items.getString(i));
            }
        }
        FileHandle.writeTxt4List(new File("/Users/snailhd/Desktop/box3.txt"),list);

//        parse();
    }


    public static List<String> parse() throws Exception{
        List<String> list = FileHandle.readText2Line("/Users/snailhd/Desktop/box1.txt",StandardCharsets.UTF_8.name());

        List<String> export = new ArrayList<>();
        for(String str:list){
            JSONObject obj = JSON.parseObject(str);
            JSONArray items = obj.getJSONObject("data").getJSONArray("items");
            for(int i=0;i<items.size();i++){
                JSONObject item = items.getJSONObject(i);
                export.add(items.getString(i));
            }
        }
        FileHandle.writeTxt4List(new File("/Users/snailhd/Desktop/box2.txt"),export);

        return export;
    }
}
